package cn.ylz.ylmall.shopping.service.impl;

import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.ex.ServiceException;
import cn.ylz.ylmall.common.web.ServiceCode;
import cn.ylz.ylmall.content.dao.persist.repository.IContentRepository;
import cn.ylz.ylmall.content.pojo.entity.Content;
import cn.ylz.ylmall.content.pojo.param.ContentAddNewParam;
import cn.ylz.ylmall.content.pojo.vo.ContentListItemVO;
import cn.ylz.ylmall.content.pojo.vo.ContentStandardVO;
import cn.ylz.ylmall.content.service.IContentService;
import cn.ylz.ylmall.shopping.dao.persist.mapper.ShoppingMapper;
import cn.ylz.ylmall.shopping.dao.persist.repository.IShoppingRepository;
import cn.ylz.ylmall.shopping.pojo.entity.Shopping;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingParam;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingQuantityParam;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingSelectedParam;
import cn.ylz.ylmall.shopping.pojo.vo.ShoppingVO;
import cn.ylz.ylmall.shopping.pojo.vo.ShoppingVO1;
import cn.ylz.ylmall.shopping.service.IShoppingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class ShoppingServiceImpl implements IShoppingService{

    @Autowired
    private IShoppingRepository iShoppingRepository;

    @Override
    public void addNew(ShoppingParam shoppingParam,Long userId) {
        Shopping shopping = new Shopping();
        log.debug("创建购物车对象");
        BeanUtils.copyProperties(shoppingParam,shopping);
        shopping.setUserId(userId);
        shopping.setNumber(0);
        shopping.setSelected(0);
        log.debug("{}",shopping);
        BigDecimal i = shoppingParam.getPrice().multiply(new BigDecimal(shopping.getQuantity()));
        log.debug("{}",i);
        shopping.setTotalPrice(i);
        int j = iShoppingRepository.insert(shopping);
    }

    @Override
    public List<ShoppingVO1> showAll(Long userId) {


        List<ShoppingVO> list = iShoppingRepository.showAll(userId);
        List<ShoppingVO1> arr = new ArrayList<>();


        for (int i = 0; i < list.size(); i++) {
            ShoppingVO1 shoppingVO1 = new ShoppingVO1();
            BeanUtils.copyProperties(list.get(i),shoppingVO1);
            Gson gson = new Gson();
            Type type = new TypeToken<List<String>>() {}.getType();
            List<String> imageUrls = gson.fromJson(list.get(i).getImageUrls(), type);
            shoppingVO1.setImageUrls(imageUrls);
            arr.add(shoppingVO1);
        }
        System.out.println(arr);
        return arr;
    }

    public void updateQuantity(List<ShoppingQuantityParam> lis){
        iShoppingRepository.updateQuantity(lis);
    }

    public void delete(List<Long> list){

        iShoppingRepository.delete(list);


    }

    @Override
    public List<ShoppingVO1> selectCartId(List<Long> list) {
        List<ShoppingVO> lis = iShoppingRepository.selectCartId(list);
        List<ShoppingVO1> arr = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ShoppingVO1 shoppingVO1 = new ShoppingVO1();
            BeanUtils.copyProperties(lis.get(i),shoppingVO1);
            Gson gson = new Gson();
            Type type = new TypeToken<List<String>>() {}.getType();
            List<String> imageUrls = gson.fromJson(lis.get(i).getImageUrls(), type);
            shoppingVO1.setImageUrls(imageUrls);
            arr.add(shoppingVO1);

        }
        System.out.println(arr);
        return arr;

    }

    @Override
    public List<ShoppingVO> showDeclineAll(Long userId) {
        return iShoppingRepository.showDeclineAll(userId);
    }

    @Override
    public List<ShoppingVO> showHavingAll(Long userId) {
        return iShoppingRepository.showHavingAll(userId);
    }


}
